package com.sgb.kjwl.viewmodel.house;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.lease.LeaseMapEntity;
import com.sgb.kjwl.model.entity.request.LeaseHallMapFilterJson;
import com.sgb.kjwl.model.entity.supplier3.DictEntity3;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.sgb.kjwl.repository.MachineRepertory;
import com.sgb.kjwl.utils.AddressHelper;
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity;
import com.sgb.kjwl.view.ui.activity.house.LeaseHallMapFragment2;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class LeaseHallMapFragModel extends BaseViewModel {
    private LeaseHallMapFragment2 fragment;
    private MachineRepertory repertory;
    private int page = 1;
    private int size = 100;
    public int total;

    public LeaseHallMapFragModel(LeaseHallMapFragment2 fragment, MachineRepertory repertory) {
        this.fragment = fragment;
        this.repertory = repertory;
    }

    public String getAreaNameModel() {
        return repertory.getAreaName();
    }

    public String getAreaCodeModel() {
        return repertory.getAreaCode();
    }

    //获取一级类型
    public void getFilterTypeModel(String type) {
        APIManager.getInstance().getLeaseMapAPI().getFistcategory(type).enqueue(new Callback<BaseEntity<List<DictEntity3>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<DictEntity3>>> call, Response<BaseEntity<List<DictEntity3>>> response) {
                if (response.body() == null || response.body().getState() == null) return;
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.showTypePopWindow(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<DictEntity3>>> call, Throwable t) {
            }
        });

    }

    //获取二级类型
    public void getFilterModelModel(String type, String parentNo) {
        APIManager.getInstance().getLeaseMapAPI().getSecondcategory(type, parentNo).enqueue(new Callback<BaseEntity<List<DictEntity3>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<DictEntity3>>> call, Response<BaseEntity<List<DictEntity3>>> response) {
                if (response.body() == null || response.body().getState() == null) {
                    fragment.showBrandPopWindow(null);
                    return;
                }
                if (APIManager.RESPONSE_OK.equals(response.body().getState())) {
                    fragment.showBrandPopWindow(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<DictEntity3>>> call, Throwable t) {
                fragment.showBrandPopWindow(null);
            }
        });
    }

//    public void getCityData() {
//        APIManager.getInstance().getMachineAPI().getAreaSecondAndThird2(repertory.getAreaCode()).enqueue(new Callback<BaseEntity<List<BaseAreaEntity<List<BaseAreaEntity>>>>>() {
//            @Override
//            public void onResponse(Call<BaseEntity<List<BaseAreaEntity<List<BaseAreaEntity>>>>> call, Response<BaseEntity<List<BaseAreaEntity<List<BaseAreaEntity>>>>> response) {
//                if (response.body() == null || response.body().getState() == null) {
//                    return;
//                }
//
//                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
//                    fragment.showAddressDatas(response.body().getData());
//                }
//            }
//
//            @Override
//            public void onFailure(Call<BaseEntity<List<BaseAreaEntity<List<BaseAreaEntity>>>>> call, Throwable t) {
//
//            }
//        });
//    }

    /**
     * 获取省市区地址
     */
    public void getCityData() {
        DialogHelper.showProgressDialog(fragment.getActivity(), "加载中...").setCanceledOnTouchOutside(false);
        AddressHelper.getInstance().getAllArea(new AddressHelper.AddressCallBack() {
            @Override
            public void initialData(List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> data) {
                DialogHelper.dismissProgressDialog();
                if (data != null && data.size() > 0) {
                    fragment.showAddressDatas(data, repertory.getAreaCode());
                }
            }
        });
    }

    //获取筛选列表
    public void getLeaseMapSearch(LeaseHallMapFilterJson filterJson, boolean isLoadMore) {
        page = isLoadMore ? page + 1 : 1;
        filterJson.setCurrent(page);
        filterJson.setLimit(size);
        if (filterJson.getAreaNos() == null) {
            filterJson.setAreaNos(new ArrayList<>());
        }
        APIManager.getInstance().getLeaseMapAPI().getLeaseMapSearch(filterJson)
                .enqueue(new Callback<BaseEntity<ListResponse<List<LeaseMapEntity>>>>() {
                    @Override
                    public void onResponse(Call<BaseEntity<ListResponse<List<LeaseMapEntity>>>> call, Response<BaseEntity<ListResponse<List<LeaseMapEntity>>>> response) {
                        if (response.body() == null || response.body().getState() == null) {
                            return;
                        }
                        if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                            fragment.showMapMark(response.body().getData().getList());
                        }

                    }

                    @Override
                    public void onFailure(Call<BaseEntity<ListResponse<List<LeaseMapEntity>>>> call, Throwable t) {
                        t.printStackTrace();
                    }
                });
    }

    public void getH5Url(int pos, String id) {
        String name = "";
        if (pos == 1) {
            name = "mechanicsDetail";//机械
        } else if (pos == 2) {
            name = "equipmentDetails";//设备
        } else if (pos == 3) {
            name = "vehicleDetails";//车辆
        } else if (pos == 4) {
            name = "propertyDetails";//房产
        } else if (pos == 5) {
            name = "landDetails";//土地
        } else if (pos == 6) {
            name = "factoryDetails";//工厂
        } else if (pos == 7) {
            name = "farmDetail";//农机
        }

        DialogHelper.showProgressDialog(fragment.getActivity(), "加载中...").setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().getUrlSetting(name).enqueue(new Callback<BaseEntity<H5ReEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<H5ReEntity>> call, Response<BaseEntity<H5ReEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    String url = response.body().getData().getUrl() + "?id=" + id + "&iosFlag=1";
//                    WebViewX5HostActivity.start(fragment.getActivity(), url);
                    WebViewX5HostActivity.start(fragment.getActivity(), url,id,"lease",pos+"");
                } else {
                    MToast.showToast(fragment.getActivity(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<H5ReEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }


}
